#include "DeviceControl.h"


DeviceControl::DeviceControl()
{
    initSystem();
    initMember();
}


void DeviceControl::initMember()
{
    currentStatus.initDefaultAll();
}

// 打印所有的初始化状态
void DeviceControl::printDeviceList()
{
    Init:       // 初始化阶段，如果有功能还在初始化，则跳转到此处

    for(MU_DEVICE_I i = MU_DEVICE_NONE;i<MU_DEVICE_ALL;++i){
        MU_INIT_TYPE type = currentStatus.GetInitType(i);
        switch (type) {
        case MU_TY_RUNNING:
            std::this_thread::sleep_for (std::chrono::seconds(1));
            /// 超时处理
            goto Init;
            break;
        case MU_TY_TRUE:
        break;
        case MU_TY_FALSE:
            std::cout << BOLDYELLOW << "有模块失败:"<< (MU_Messages::Module[i])<< RESET<<std::endl;
        break;
        case MU_TY_NONE:
            std::cout << BOLDYELLOW << "有模块没有开始初始化:"<< (MU_Messages::Module[i])<< RESET<<std::endl;
        break;
        case MU_TY_STOP:
            std::cout << BOLDYELLOW << "有模块意外停止:"<< (MU_Messages::Module[i])<< RESET<<std::endl;
        break;
        default:
            std::cout << BOLDYELLOW << "超出范围:"<< (MU_Messages::Module[i])<< RESET<<std::endl;
            break;
        }
    }

}


/**
 * @brief DeviceControl::initialDevice
 * @details 支持了选择性初始化模块，以前是所有模块都要初始化
 * @return
 */
MU_RETURNS DeviceControl::initialDevice()
{
    initNoUse();        /// 初始化不需要初始化的模块

    initing.sql ? (sqlProvider::GetInstance()->Connect(),reflushData()):undo();
    initing.cam ? CameraHelper::GetInstance()->Connect():undo();
    initing.laser ? LaserHelper::GetInstance()->Connect():undo();
    initing.machine ? MachineHelper::GetInstance()->Connect():undo();
    initing.plc ? PLCHelper::GetInstance()->Connect():undo();
    initing.robot ? RobotHelper::GetInstance()->ConnectToRobot():undo();

    bool allInited = true;
    printDeviceList();

    inited.sql ? (allInited &= currentStatus.GetInit(MU_DEVICE_SQL)) : (allInited &= true);
    inited.cam ? (allInited &= currentStatus.GetInit(MU_DEVICE_CAM)) : (allInited &= true);
    inited.laser ? (allInited &= currentStatus.GetInit(MU_DEVICE_LASYER)) : (allInited &= true);
    inited.machine ? (allInited &= currentStatus.GetInit(MU_DEVICE_MACHINE)) : (allInited &= true);
    inited.plc ? (allInited &= currentStatus.GetInit(MU_DEVICE_PLC)) : (allInited &= true);
    inited.robot ? (allInited &= currentStatus.GetInit(MU_DEVICE_ROBOT)) : (allInited &= true);

    if(allInited){
        goto Successed;
    }else{
        goto Errored;
    }

    Successed:{
        std::cout << BOLDYELLOW << "进入Successed 处理:"<< RESET<<std::endl;
        currentStatus.status.exchange(MU_STATUS_SUCCESS);
        currentStatus.expLock.store(0);
        return  MU_RETURN_SUCCESS;
    }
    Errored:{
        std::cout << BOLDYELLOW << "进入Errored 处理:"<< RESET<<std::endl;
        currentStatus.status.exchange(MU_STATUS_CRASH);
        currentStatus.expLock.store(0);
        return  MU_RETURN_ERROR;
    }

    return  MU_RETURN_SUCCESS;
}

/**
 * @brief DeviceControl::initSystem
 * @return MU_RETURNS
 * @attention 此处初始化日志模块，及创建文件夹
 * @date 2020-04-08
 */
MU_RETURNS DeviceControl::initSystem()
{
    _mkdir("grid");     /*  创建格点文件 */
    currentStatus.curVer = APP_VERSION;
    initing.init();  // 检测是否需要初始化
    inited.inited();

    // 1.初始化日志输出模块
    return MU_RETURN_SUCCESS;
}

/// 设置不需要初始化的模块
void DeviceControl::initNoUse()
{
    currentStatus.initSucess(MU_DEVICE_IO);
    currentStatus.initSucess(MU_DEVICE_IMAGE);
    currentStatus.initSucess(MU_DEVICE_WELD);
    currentStatus.initSucess(MU_DEVICE_CALC);
    currentStatus.initSucess(MU_DEVICE_LOG);
    currentStatus.initSucess(MU_DEVICE_NONE);

    //currentStatus.initSucess(MU_DEVICE_LASYER);
    // currentStatus.initSucess(MU_DEVICE_SQL);
    //currentStatus.initSucess(MU_DEVICE_MACHINE);
    // currentStatus.initSucess(MU_DEVICE_CAM);
    // currentStatus.initSucess(MU_DEVICE_PLC);
    // currentStatus.initSucess(MU_DEVICE_ROBOT);
}


// 获取安全点
void DeviceControl::reflushData()
{
    auto t = SQLHelper::GetBlade()->_p_tr->Get({"SAFEPOS"},{"POS"},{"POSID","'0'"});
    try{
        QString strP        = t.at(0).toString().section(",",0,5);
        QString str_axis    = t.at(0).toString().section(",",6,-1);
        RobotHelper::GetInstance()->defaultAxle = RobotAxle::instance(str_axis.toStdString());
        RobotHelper::GetInstance()->defaultPos = RobotPos::instance(strP.toStdString());
    }catch(...){
        qWarning()<<BOLDRED<<__FUNCTION__<<"数据库可能没有该字段，请检查当前数据库版本!"<<RESET;
    }
}
